package com.mobi.Survey.lib.model;

import java.io.File;
import java.util.Comparator;
import java.util.List;

import android.content.Context;
import android.util.Log;

import com.mobi.Survey.lib.db.AbstractTable;
import com.mobi.Survey.lib.db.SurveyDetailTable;
import com.mobi.Survey.lib.uicomponents.ControlFactory;
import com.mobi.Survey.lib.utils.Device;
import com.mobi.Survey.lib.utils.Helper;

/**
 * 
 * <b>Author:</b> Yahya Arshad
 * <p>
 * <b>Company:</b> Mobi Media Soft
 * <p>
 * <b> Website:</b> www.mobimediasoft.com
 * <p>
 * SurveyDetailPojo.java class holds data about individual control and saving it
 */
public class SurveyDetailPojo extends Pojo {

	private String ctrlResult;

	private byte controlType;
	private boolean isMendatory;

	private String controlText;
	private String controlParameter1;
	private String controlParameter2;

	private String actualValue;
	private String userValue;
	private String defaultValue;
	private float percentHeight;
	private float percentWidth;
	private int sequanceNo;

	private long surveyId;
	private long id;
	private long subscriptionId;

	/* Position Variables are Reserved for Future */
	private float positionX;
	private float positionY;

	private SurveyPojo survey;

	public String getControlParameter2() {
		return controlParameter2;
	}

	public void setControlParameter2(String controlParameter2) {
		this.controlParameter2 = controlParameter2;
	}

	public SurveyPojo getSurvey() {
		return survey;
	}

	public void setSurvey(SurveyPojo survey) {
		this.survey = survey;
	}

	public SurveyDetailPojo() {

	}

	public SurveyDetailPojo(byte controlType, String controlText, String controlParameter1, long subscriptionId,
			String actualValue) {
		super();
		this.controlType = controlType;
		this.controlText = controlText;
		this.controlParameter1 = controlParameter1;
		this.subscriptionId = subscriptionId;
		this.actualValue = actualValue;

	}

	public static final class SortBySequance implements Comparator<SurveyDetailPojo> {

		@Override
		public int compare(SurveyDetailPojo lhs, SurveyDetailPojo rhs) {

			return lhs.getSequanceNo() - rhs.getSequanceNo();
		}

	}

	public long getId() {
		return id;
	}

	public SurveyDetailPojo setId(long id) {
		this.id = id;
		return this;
	}

	public long getSurveyId() {
		return surveyId;
	}

	public byte getControlType() {
		return controlType;
	}

	public String getControlText() {
		return controlText;
	}

	public String getControlParameter1() {

		return (controlParameter1 == null) ? "" : controlParameter1;
	}

	public static final String getDeviceId() {
		return Device.getDeviceId();
	}

	public long getSubscriptionId() {
		return subscriptionId;
	}

	public SurveyDetailPojo setControlType(byte controlType) {
		this.controlType = controlType;
		return this;
	}

	public SurveyDetailPojo setControlText(String controlText) {
		this.controlText = controlText;
		return this;
	}

	public SurveyDetailPojo setControlParameter1(String controlParameter1) {
		this.controlParameter1 = controlParameter1;
		return this;
	}

	public SurveyDetailPojo setSubscriptionId(long subscriptionId) {
		this.subscriptionId = subscriptionId;
		return this;
	}

	/**
	 * Saves survey control data in databases
	 * 
	 * @param ctx
	 *            Context of class
	 * @param surveyDetailPojos
	 *            Collection of SurveyDetailPojo of survey
	 * @param surveyId
	 *            id of survey to which detail belongs
	 * @return
	 */
	public static long commit(Context ctx, List<SurveyDetailPojo> surveyDetailPojos, long surveyId) {
		AbstractTable at = new SurveyDetailTable(ctx);

		try {

			for (SurveyDetailPojo detail : surveyDetailPojos) {
				at.insertRow(detail, surveyId);

			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {

			at.closeDB();

		}

		return 1;
	}

	/**
	 * return result value of control
	 * 
	 * @return
	 */
	public String getCtrlResult() {
		return ctrlResult;
	}

	public void setCtrlResult(String ctrlResult) {
		this.ctrlResult = ctrlResult;
	}

	public SurveyDetailPojo setSurveyId(long sId) {

		surveyId = sId;
		return this;
	}

	public boolean isMendatory() {
		return isMendatory;
	}

	public SurveyDetailPojo setMendatory(boolean isMendatory) {
		this.isMendatory = isMendatory;
		return this;
	}

	public String getActualValue() {
		if (actualValue == null)
			return "";
		return actualValue;
	}

	public String getUserValue() {
		if (userValue == null)
			return "";
		return userValue;
	}

	public SurveyDetailPojo setActualValue(String actualValue) {
		this.actualValue = actualValue;
		return this;
	}

	public SurveyDetailPojo setUsertValue(String userValue) {
		this.userValue = userValue;
		return this;
	}

	public int getSequanceNo() {
		return sequanceNo;
	}

	public SurveyDetailPojo setSequanceNo(int sequanceNo) {
		this.sequanceNo = sequanceNo;
		return this;
	}

	public String getDefaultValue() {
		if (defaultValue == null)
			return "";
		return defaultValue;
	}

	public SurveyDetailPojo setUserValue(String userValue) {
		this.userValue = userValue;
		return this;

	}

	/**
	 * Default value for Controls(CustomEditText,
	 * CustomTimePicker,CustomDatePicker)
	 * 
	 * @param defaultValue
	 * @return
	 */
	public SurveyDetailPojo setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
		return this;
	}

	public float getPercentHeight() {
		return percentHeight;
	}

	public SurveyDetailPojo setPercentHeight(float percentHeight) {
		this.percentHeight = percentHeight;
		return this;
	}

	public float getPercentWidth() {
		return percentWidth;
	}

	public boolean isSaveAble() {
		return (controlType != ControlFactory.CONTROL_IMAGE_VIEW && controlType != ControlFactory.CONTROL_VIDEO_VIEW
				&& controlType != ControlFactory.CONTROL_TYPE_ZERO && controlType != ControlFactory.CONTROL_URL
				&& controlType != ControlFactory.CONTROL_EMAIL && controlType != ControlFactory.CONTROL_PHONE && controlType != ControlFactory.CONTROL_ADDRESS);
	}

	public SurveyDetailPojo setPercentWidth(float percentWidth) {
		this.percentWidth = percentWidth;
		return this;
	}

	public float getPositionX() {
		return positionX;
	}

	public void setPositionX(float positionX) {
		this.positionX = positionX;
	}

	public float getPositionY() {
		return positionY;
	}

	public void setPositionY(float positionY) {
		this.positionY = positionY;
	}

	public static void deleteAllDetailBySurveyId(Context ctx, long surveyId) {
		AbstractTable at = new SurveyDetailTable(ctx);
		try {
			at.deleteRow(surveyId);

		} catch (Exception e) {

		} finally {
			at.closeDB();
		}
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "Seq: " + sequanceNo + ",Mandatory: " + isMendatory;
	}

}
